package com.neu.edu.boonpay.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.neu.edu.boonpay.dao.BalanceDao;
import com.neu.edu.boonpay.pojo.Balance;
import com.neu.edu.boonpay.service.BalanceService;
import com.neu.edu.boonpay.utils.PartyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class BalanceServiceImpl implements BalanceService {
    @Autowired
    private BalanceDao balanceDao;
    @Override
    public String getBalance(Integer userid, String token) {
        try {
            /*String apiUrl = String.format(
                    "https://status.rechargeexchange.com/API.asmx/BalanceNew?userid=%s&token=%s",
                    userid,token
            );
            String balances = PartyUtil.sendRequest(apiUrl, "POST");*/
            String balances = "{\n" +
                    "        \"status\":\"SUCCESS\",\n" +
                    "\t\"message\":\"Balance Found.\",\n" +
                    "\t\"balance\":\"1516.30\",\n" +
                    "\t\"sellbalance\":\"116.30\",\n" +
                    "        }";
            Balance balance = new Balance();
            balance.setUserid(userid);
            balance.setToken(token);
            JSONObject jsonObject = JSON.parseObject(balances);
            Object status = jsonObject.get("status");
            if (status != null) {
                balance.setStatus((String) status);
            }
            Object message = jsonObject.get("message");
            if (message != null) {
                balance.setMessage((String) message);
            }
            Object balanceEntity = jsonObject.get("balance");
            if (balanceEntity != null) {
                balance.setBalance(balanceEntity.toString());
            }
            Object sellbalance = jsonObject.get("sellbalance");
            if (sellbalance != null) {
                balance.setSellbalance(sellbalance.toString());
            }
            //balanceDao.insert(balance);
            return balances;
        }catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
        }
        return "";
    }


}
